<?php
class Database {
    public static function connect() {
        $dsn = 'mysql:dbname=myfirstdatabase;host=127.0.0.1';
        $user = 'root';
        $password = '';
        $dbh = null;
        try {
            $dbh = new PDO($dsn, $user, $password);
            $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        } catch (PDOException $e) {
            echo 'Connexion échouée : ' . $e->getMessage();
            exit(0);
        }
        return $dbh;
    }


}

class Utilisateur {
    public $login;
    public $mdp;
    public $nom;
    public $prenom;
    public $promotion;
    public $naissance;
    public $email;
    public $feuille;

    public static function getUtilisateur($login) {
        $dbh = Database::connect();
        $query = "SELECT * FROM `utilisateurs` WHERE `login` = ?";
        $sth = $dbh->prepare($query);
        $sth->execute(array($login));
        $sth->setFetchMode(PDO::FETCH_CLASS, 'Utilisateur');
        $user = $sth->fetch(PDO::FETCH_CLASS);
        if ($sth) return $user; else return null;
    }

    public static function verifyUser($login,$pwd) {
        $dbh = Database::connect();
        $query = "SELECT * FROM `utilisateurs` WHERE `login` = ? AND `mdp` = SHA1(?)";
        $sth = $dbh->prepare($query);
        $sth->execute(array($login,$pwd));
        return $sth->rowCount()==0 ? false : true;
    }

//    public static function insererUtilisateur($login,$mdp,$nom,$prenom,$promotion,$naissance,$email,$feuille) {
//        $dbh = Database::connect();
//        $query = "INSERT INTO `utilisateurs` (`login`, `mdp`, `nom`, `prenom`, `promotion`, `naissance`, `email`, `feuille`) VALUES(?,SHA1(?),?,?,?,?,?,?)";
//        $sth = $dbh->prepare($query);
//        $sth->execute(array($login,$mdp,$nom,$prenom,$promotion,$naissance,$email,$feuille));
//    }

    public function __toString() {
        return $this->prenom.' '.$this->nom;
    }
}



//// opérations sur la base
//$dbh = Database::connect();
////$dbh->query("INSERT INTO `utilisateurs` (`login`, `mdp`, `nom`, `prenom`, `promotion`, `naissance`, `email`, `feuille`) VALUES('moi',SHA1('nombril'),'bebe','louis','2005','1980-03-27','Marcel.Dupont@polytechnique.edu','modex1.css')");
////$sth = $dbh->prepare("INSERT INTO `utilisateurs` (`login`, `mdp`, `nom`, `prenom`, `promotion`, `naissance`, `email`, `feuille`) VALUES(?,SHA1(?),?,?,?,?,?,?)");
//Utilisateur::insererUtilisateur('SuperMarcel','Mystere','Marcel','Dupont','2005','1980-03-27','Marcel.Dupont@polytechnique.edu','modex1.css');
//
//$query = "SELECT * FROM `utilisateurs`";
//$sth = $dbh->prepare($query);
//$sth->execute();
//$sth->setFetchMode(PDO::FETCH_CLASS, 'Utilisateur');
//
////$user2->email = 'fdafd';
////$dbh = null; // Déconnexion de MySQL
//while ($user =  $sth->fetch(PDO::FETCH_CLASS)) {
//    $birth = explode('-', $user->naissance);
//    echo '['.$user->login.'] '.$user->prenom.' <b>'.$user->nom.'</b>, né le '.$birth[2].'/'.$birth[1].'/'.$birth[0].', '.(($user->promotion)?$user->promotion.', ':'').$user->email.'<br/>';
//}
//$sth->closeCursor();
//echo "what the hell?";
//$dbh->prepare("SELECT * FROM `utilisateurs` WHERE `nom` = 'Rossin'");

//echo Utilisateur::getUtilisateur('DRossin');

?>
